package cn.arikeky.tran;

import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class SyncProducer {
	public static void main(String[] args) throws Exception {
    	// 实例化消息生产者Producer
		TransactionMQProducer producer = new TransactionMQProducer("tran_group_name");
		// 设置NameServer的地址
    	producer.setNamesrvAddr("localhost:9876");
    	// 调用监听器
    	producer.setTransactionListener(new TranListener());
    	// 启动Producer实例
        producer.start();
		// 创建消息，并指定Topic，Tag和消息体
		Message msg = new Message("Topic_tran",
		"Pet_tran",
		"事务提交消息~~~~~~~！ ".getBytes(StandardCharsets.UTF_8)
		);
		// 发送同步消息到一个Broker
		TransactionSendResult transactionSendResult = producer.sendMessageInTransaction(msg, "");
		// 通过sendResult返回消息是否成功送达
		System.out.printf("%s%n", transactionSendResult);

		// 查看同步消息是否发送成功
		System.out.println(transactionSendResult.getSendStatus());
    	// 如果不再发送消息，关闭Producer实例。
    	producer.shutdown();
    }
}